﻿#region Using Directives

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;

    using Csla;
    using Csla.Data;
    using Csla.Server;
    using Csla.Reflection;

    using Common = MarketPulse.BusinessLogic.Common;
    using MarketPulse.BusinessLogic.Common.Criteria;

    using MarketPulse.DataAccess.DataContextObjects.Production;

#endregion

namespace MarketPulse.DataAccess.Factory
{
    public class EnquiryFactory : ObjectFactory
    {
        #region Member Variables

            /// <summary>
            /// Property that stores the name of the class.
            /// </summary>
            private static readonly Type mClass = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType;

            /// <summary>
            /// Property that stores the log4net logger for this class.
            /// </summary>
            private static readonly log4net.ILog mLog = log4net.LogManager.GetLogger(mClass);

        #endregion

        #region Methods

            #region Create Methods

                /// <summary>
                /// Method used to create and return a new Enquiry object instance
                /// </summary>
                /// <returns>The newly created Enquiry object</returns>
                [RunLocal]
                public Common.Enquiry Create()
                {
                    // Create a new instance of the object
                    var enquiry = (Common.Enquiry)MethodCaller.CreateInstance(typeof(Common.Enquiry));

                    enquiry.Obsolete = false;

                    // Mark it as a new object
                    MarkNew(enquiry);
                    CheckRules(enquiry);

                    return enquiry;
                }

            #endregion

            #region Fetch Methods

                #region Enquiry

                    /// <summary>
                    /// Method used to retrieve an existing Enquiry from the database based on the Guid supplied
                    /// </summary>
                    /// <param name="criteria">The criteria object holding the Guid of the Program to retrieve</param>
                    /// <returns>The Enquiry object retrieved from the database</returns>
                    public Common.Enquiry Fetch(SingleCriteria<Guid> criteria)
                    {

                        try
                        {
                            using (var manager = ContextManager<MUM_dev>.GetManager(Database.BusinessSystem))
                            {
                                // Create an instance to return
                                var enquiry = (Common.Enquiry)MethodCaller.CreateInstance(typeof(Common.Enquiry));

                                // Fetch the data from the database based on the criteria
                                var selectResult = manager.DataContext.UspEnquirySelectByGuid(criteria.Value);

                                // Get the result
                                UspEnquirySelectByGuidResult result = null;
                                try
                                {
                                    result = selectResult.Single();
                                }
                                catch (InvalidOperationException ioe)
                                {
                                    // Construct error message
                                    StringBuilder errorMessage = new StringBuilder();
                                    errorMessage.Append("Unable to locate exactly one Enquiry with Guid = [ ");
                                    errorMessage.Append(criteria.Value);
                                    errorMessage.Append(" ]");
                                    mLog.Error(errorMessage.ToString(), ioe);
                                    throw new DataAccessException(errorMessage.ToString());
                                }

                                // Common properties inherited from MarketPulseBase<Enquiry>
                                LoadProperty(enquiry, Common.Enquiry.GUIDProperty, criteria.Value);
                                LoadProperty(enquiry, Common.Enquiry.InsertDateTimeProperty, result.InsertDateTime.Value);
                                LoadProperty(enquiry, Common.Enquiry.InsertUserGUIDProperty, result.InsertUserGUID.Value);
                                LoadProperty(enquiry, Common.Enquiry.UpdateDateTimeProperty, result.UpdateDateTime.Value);
                                LoadProperty(enquiry, Common.Enquiry.UpdateUserGUIDProperty, result.UpdateUserGUID.Value);
                                // Other properties
                                LoadProperty(enquiry, Common.Enquiry.EnquiryReasonGUIDProperty, result.EnquiryReasonGUID.Value);
                                LoadProperty(enquiry, Common.Enquiry.EnquiryTypeGUIDProperty, result.EnquiryTypeGUID.Value);
                                LoadProperty(enquiry, Common.Enquiry.EnquiryXMLProperty, result.EnquiryXML.ToString());
                                LoadProperty(enquiry, Common.Enquiry.VisitGUIDProperty, result.VisitGUID.Value);

                                // Make sure that CSLA knows this has changed since being instantiated
                                MarkOld(enquiry);

                                return enquiry;
                            }
                        }
                        catch (DataAccessException dae)
                        {
                            mLog.Error(dae.ToString());
                            throw dae;
                        }
                        catch (Exception ex)
                        {
                            mLog.Error(ex.ToString());
                            throw ex;
                        }
                    }

                #endregion

                #region EnquiryList

                    /// <summary>
                    /// Method used to retrieve a list of Enquiries from the database based on the criteria supplied
                    /// </summary>
                    /// <param name="criteria">The criteria object holding the Guid of the EnquiryList to retrieve</param>
                    /// <returns>The EnquiryList object retrieved from the database</returns>
                    public Common.EnquiryList FetchList(EnquiryCriteria enquiryCriteria)
                    {
                        try
                        {
                            using (var manager = ContextManager<MUM_dev>.GetManager(Database.BusinessSystem))
                            {
                                var enquiryList = (Common.EnquiryList)MethodCaller.CreateInstance(typeof(Common.EnquiryList));

                                var selectResult = manager.DataContext.UspEnquirySelectByProfileGUIDAndCompanyGUID(enquiryCriteria.ProfileGUID, enquiryCriteria.CompanyGUID);

                                foreach (UspEnquirySelectByProfileGUIDAndCompanyGUIDResult result in selectResult)
                                {
                                    Common.Enquiry enquiry = (Common.Enquiry)MethodCaller.CreateInstance(typeof(Common.Enquiry));

                                    // Mark as child object?? "List item must be marked as a child object"
                                    MarkAsChild(enquiry);

                                    LoadProperty(enquiry, Common.Enquiry.GUIDProperty, result.EnquiryGUID.Value);
                                    LoadProperty(enquiry, Common.Enquiry.InsertDateTimeProperty, (SmartDate)result.InsertDateTime.Value);
                                    LoadProperty(enquiry, Common.Enquiry.InsertUserGUIDProperty, result.InsertUserGUID.Value);
                                    LoadProperty(enquiry, Common.Enquiry.UpdateDateTimeProperty, (SmartDate)result.UpdateDateTime.Value);
                                    LoadProperty(enquiry, Common.Enquiry.UpdateUserGUIDProperty, result.UpdateUserGUID.Value);
                                    // Other properties
                                    LoadProperty(enquiry, Common.Enquiry.EnquiryReasonGUIDProperty, result.EnquiryReasonGUID.Value);
                                    LoadProperty(enquiry, Common.Enquiry.EnquiryTypeGUIDProperty, result.EnquiryTypeGUID.Value);
                                    LoadProperty(enquiry, Common.Enquiry.EnquiryXMLProperty, result.EnquiryXML.ToString());
                                    LoadProperty(enquiry, Common.Enquiry.VisitGUIDProperty, result.VisitGUID.Value);

                                    enquiryList.RaiseListChangedEvents = false;

                                    // Add the item to the list
                                    enquiryList.Add(enquiry);

                                    enquiryList.RaiseListChangedEvents = true;
                                }
                                // Make sure that CSLA knows this has changed since being instantiated
                                MarkOld(enquiryList);

                                return enquiryList;
                            }
                        }
                        catch (InvalidOperationException ioe)
                        {
                            mLog.Error(ioe.ToString());
                            throw ioe;
                        }
                        catch (Exception ex)
                        {
                            mLog.Error(ex.ToString());
                            throw ex;
                        }
                    }

                #endregion

                #region EnquiryTypeInfo

                    /// <summary>
                    /// Method used to fetch a Read Only copy of an existing EnquiryTypeInfo from the database given a guid.
                    /// </summary>
                    /// <param name="criteria">The criteria object holding the Guid of the EnquiryTypeInfo to retrieve</param>
                    /// <returns>The EnquiryTypeInfo object retrieved from the database</returns>
                    public Common.EnquiryTypeInfo FetchTypeInfo(SingleCriteria<Guid> criteria)
                    {
                        try
                        {
                            using (var mgr = ContextManager<MUM_dev>.GetManager(Database.BusinessSystem))
                            {

                                // Create an instance to return
                                var enquiryTypeInfo = (Common.EnquiryTypeInfo)MethodCaller.CreateInstance(typeof(Common.EnquiryTypeInfo));

                                // Fetch the data from the database based on the criteria
                                var selectResult = mgr.DataContext.UspEnquiryTypeSelectByGuid(criteria.Value);

                                // Get the result
                                UspEnquiryTypeSelectByGuidResult result = null;
                                try
                                {
                                    result = selectResult.Single();
                                }
                                catch (InvalidOperationException ioe)
                                {
                                    // Construct error message
                                    StringBuilder errorMessage = new StringBuilder();
                                    errorMessage.Append("Unable to locate exactly one EnquiryType with Guid = [ ");
                                    errorMessage.Append(criteria.Value);
                                    errorMessage.Append(" ]");
                                    mLog.Error(errorMessage.ToString(), ioe);
                                    throw new DataAccessException(errorMessage.ToString());
                                }

                                // Common properties inherited from MarketPulseBase<Branch>
                                LoadProperty(enquiryTypeInfo, Common.EnquiryTypeInfo.GUIDProperty, criteria.Value);
                                LoadProperty(enquiryTypeInfo, Common.EnquiryTypeInfo.ObsoleteProperty, result.Obsolete.Value);
                                LoadProperty(enquiryTypeInfo, Common.EnquiryTypeInfo.InsertDateTimeProperty, result.InsertDateTime.Value);
                                LoadProperty(enquiryTypeInfo, Common.EnquiryTypeInfo.InsertUserGUIDProperty, result.InsertUserGUID.Value);
                                LoadProperty(enquiryTypeInfo, Common.EnquiryTypeInfo.UpdateDateTimeProperty, result.UpdateDateTime.Value);
                                LoadProperty(enquiryTypeInfo, Common.EnquiryTypeInfo.UpdateUserGUIDProperty, result.UpdateUserGUID.Value);
                                // Other properties
                                LoadProperty(enquiryTypeInfo, Common.EnquiryTypeInfo.mLabelProperty, result.EnquiryTypeLabel);


                                // Make sure that CSLA knows this has changed since being instantiated
                                MarkOld(enquiryTypeInfo);

                                return enquiryTypeInfo;
                            }
                        }
                        catch (DataAccessException dae)
                        {
                            mLog.Error(dae.ToString());
                            throw dae;
                        }
                        catch (Exception ex)
                        {
                            mLog.Error(ex.ToString());
                            throw ex;
                        }
                    }

                #endregion

                #region EnquiryTypeInfoList

                    /// <summary>
                    /// Method used to fetch a read only list of read only EnquiryType objects from the database based on the criteria supplied             
                    /// </summary>
                    /// <param name="criteria">The criteria object holding the values used to retrieve the list</param>
                    /// <returns>A read only list of read only EnquiryTypeInfo objects retrieved from the database</returns>
                    public Common.EnquiryTypeInfoList FetchTypeInfoList(SingleCriteria<Guid> criteria)
                    {
                        try
                        {
                            using (var manager = ContextManager<MUM_dev>.GetManager(Database.BusinessSystem))
                            {
                                // Create an instance to return
                                var enquiryTypeInfoList = (Common.EnquiryTypeInfoList)MethodCaller.CreateInstance(typeof(Common.EnquiryTypeInfoList));

                                // Fetch the data from the database based on the criteria
                                var selectResult = manager.DataContext.UspEnquiryTypeSelectByClientGUID(criteria.Value);

                                // VisitInfoList is a read-only collection,
                                // so typically, when the data access is implemented in
                                // DataPortal_Fetch within the business object, the
                                // private IsReadOnly property must be set false in order to 
                                // populate the collection.  Here, within a factory 
                                // , it isn't visible so one must resort to calling the 
                                // SetIsReadOnly method provided by our parent, ObjectFactory.
                                SetIsReadOnly(enquiryTypeInfoList, false);
                                foreach (UspEnquiryTypeSelectByClientGUIDResult result in selectResult)
                                {
                                    var enquiryTypeInfo = (Common.EnquiryTypeInfo)MethodCaller.CreateInstance(typeof(Common.EnquiryTypeInfo));

                                    // Common properties inherited from MarketPulseBase
                                    LoadProperty(enquiryTypeInfo, Common.EnquiryTypeInfo.GUIDProperty, result.EnquiryTypeGUID.Value);
                                    LoadProperty(enquiryTypeInfo, Common.EnquiryTypeInfo.ObsoleteProperty, result.Obsolete.Value);
                                    LoadProperty(enquiryTypeInfo, Common.EnquiryTypeInfo.InsertDateTimeProperty, result.InsertDateTime.Value);
                                    LoadProperty(enquiryTypeInfo, Common.EnquiryTypeInfo.InsertUserGUIDProperty, result.InsertUserGUID.Value);
                                    LoadProperty(enquiryTypeInfo, Common.EnquiryTypeInfo.UpdateDateTimeProperty, result.UpdateDateTime.Value);
                                    LoadProperty(enquiryTypeInfo, Common.EnquiryTypeInfo.UpdateUserGUIDProperty, result.UpdateUserGUID.Value);
                                    // Other properties
                                    LoadProperty(enquiryTypeInfo, Common.EnquiryTypeInfo.mLabelProperty, result.EnquiryTypeLabel);

                                    // Make sure that CSLA knows this has changed since being instantiated
                                    MarkOld(enquiryTypeInfo);

                                    // Add the item to the list
                                    enquiryTypeInfoList.Add(enquiryTypeInfo);
                                }
                                SetIsReadOnly(enquiryTypeInfoList, true);

                                // Make sure that CSLA knows this has changed since being instantiated
                                MarkOld(enquiryTypeInfoList);

                                return enquiryTypeInfoList;
                            }
                        }
                        catch (InvalidOperationException ioe)
                        {
                            mLog.Error(ioe.ToString());
                            throw ioe;
                        }
                        catch (Exception ex)
                        {
                            mLog.Error(ex.ToString());
                            throw ex;
                        }
                    }

                #endregion

                #region EnquiryReasonInfo

                    /// <summary>
                    /// Method used to fetch a Read Only copy of an existing EnquiryReason from the database given a guid.
                    /// </summary>
                    /// <param name="criteria">The criteria object holding the EnquiryReasonGuid of the EnquiryReason to retrieve</param>
                    /// <returns>The EnquiryReason object retrieved from the database</returns>
                    public Common.EnquiryReasonInfo FetchReasonInfo(SingleCriteria<Guid> criteria)
                    {
                        try
                        {
                            using (var mgr = ContextManager<MUM_dev>.GetManager(Database.BusinessSystem))
                            {

                                // Create an instance to return
                                var enquiryReasonInfo = (Common.EnquiryReasonInfo)MethodCaller.CreateInstance(typeof(Common.EnquiryReasonInfo));

                                // Fetch the data from the database based on the criteria
                                var selectResult = mgr.DataContext.UspEnquiryReasonSelectByGuid(criteria.Value);

                                // Get the result
                                UspEnquiryReasonSelectByGuidResult result = null;
                                try
                                {
                                    result = selectResult.Single();
                                }
                                catch (InvalidOperationException ioe)
                                {
                                    // Construct error message
                                    StringBuilder errorMessage = new StringBuilder();
                                    errorMessage.Append("Unable to locate exactly one EnquiryReason with Guid = [ ");
                                    errorMessage.Append(criteria.Value);
                                    errorMessage.Append(" ]");
                                    mLog.Error(errorMessage.ToString(), ioe);
                                    throw new DataAccessException(errorMessage.ToString());
                                }

                                // Common properties inherited from MarketPulseBase<Branch>
                                LoadProperty(enquiryReasonInfo, Common.EnquiryReasonInfo.GUIDProperty, criteria.Value);
                                LoadProperty(enquiryReasonInfo, Common.EnquiryReasonInfo.ObsoleteProperty, result.Obsolete.Value);
                                LoadProperty(enquiryReasonInfo, Common.EnquiryReasonInfo.InsertDateTimeProperty, result.InsertDateTime.Value);
                                LoadProperty(enquiryReasonInfo, Common.EnquiryReasonInfo.InsertUserGUIDProperty, result.InsertUserGUID.Value);
                                LoadProperty(enquiryReasonInfo, Common.EnquiryReasonInfo.UpdateDateTimeProperty, result.UpdateDateTime.Value);
                                LoadProperty(enquiryReasonInfo, Common.EnquiryReasonInfo.UpdateUserGUIDProperty, result.UpdateUserGUID.Value);
                                // Other properties
                                LoadProperty(enquiryReasonInfo, Common.EnquiryReasonInfo.mEnquiryTypeGUIDProperty, result.EnquiryTypeGUID.Value);
                                LoadProperty(enquiryReasonInfo, Common.EnquiryReasonInfo.mLabelProperty, result.EnquiryReasonLabel);
                                LoadProperty(enquiryReasonInfo, Common.EnquiryReasonInfo.mAdditionalInfoTextProperty, result.EnquiryReasonAdditionalInfoText);

                                if (result.CompanyGUID.HasValue)
                                {
                                    LoadProperty(enquiryReasonInfo, Common.EnquiryReasonInfo.mCompanyGUIDProperty, result.CompanyGUID.Value);
                                }

                                // Make sure that CSLA knows this has changed since being instantiated
                                MarkOld(enquiryReasonInfo);

                                return enquiryReasonInfo;
                            }
                        }
                        catch (DataAccessException dae)
                        {
                            mLog.Error(dae.ToString());
                            throw dae;
                        }
                        catch (Exception ex)
                        {
                            mLog.Error(ex.ToString());
                            throw ex;
                        }
                    }

                #endregion

                #region EnquiryReasonInfoList

                    /// <summary>
                    /// Method used to fetch a read only list of read only EnquiryReasonInfo objects from the database based on the criteria supplied
                    /// </summary>
                    /// <param name="criteria">The criteria object holding the Guid of the client to retrieve retrieve the reason list for</param>
                    /// <returns>A read only list of read only EnquiryReasonInfo objects retrieved from the database</returns>
                    public Common.EnquiryReasonInfoList FetchReasonInfoList(SingleCriteria<Guid> criteria)
                    {
                        try
                        {
                            using (var manager = ContextManager<MUM_dev>.GetManager(Database.BusinessSystem))
                            {
                                // Create an instance to return
                                var enquiryReasonInfoList = (Common.EnquiryReasonInfoList)MethodCaller.CreateInstance(typeof(Common.EnquiryReasonInfoList));

                                // Fetch the data from the database based on the criteria
                                //TODO refactor to include a clientGUID or NULL for specific and generi enquiry reason retrieval
                                var selectResult = manager.DataContext.UspEnquiryReasonSelectByClientGUID(criteria.Value);

                                // EnquiryReasonInfoList is a read-only collection,
                                // so typically, when the data access is implemented in
                                // DataPortal_Fetch within the business object, the
                                // private IsReadOnly property must be set false in order to 
                                // populate the collection.  Here, within a factory 
                                // , it isn't visible so one must resort to calling the 
                                // SetIsReadOnly method provided by our parent, ObjectFactory.
                                SetIsReadOnly(enquiryReasonInfoList, false);
                                foreach (UspEnquiryReasonSelectByClientGUIDResult result in selectResult)
                                {
                                    var enquiryReasonInfo = (Common.EnquiryReasonInfo)MethodCaller.CreateInstance(typeof(Common.EnquiryReasonInfo));

                                    // Common properties inherited from MarketPulseBase<EnquiryTypeInfo>
                                    LoadProperty(enquiryReasonInfo, Common.EnquiryReasonInfo.GUIDProperty, result.EnquiryReasonGUID.Value);
                                    LoadProperty(enquiryReasonInfo, Common.EnquiryReasonInfo.ObsoleteProperty, result.Obsolete.Value);
                                    LoadProperty(enquiryReasonInfo, Common.EnquiryReasonInfo.InsertDateTimeProperty, result.InsertDateTime.Value);
                                    LoadProperty(enquiryReasonInfo, Common.EnquiryReasonInfo.InsertUserGUIDProperty, result.InsertUserGUID.Value);
                                    LoadProperty(enquiryReasonInfo, Common.EnquiryReasonInfo.UpdateDateTimeProperty, result.UpdateDateTime.Value);
                                    LoadProperty(enquiryReasonInfo, Common.EnquiryReasonInfo.UpdateUserGUIDProperty, result.UpdateUserGUID.Value);
                                    // Other properties                    
                                    LoadProperty(enquiryReasonInfo, Common.EnquiryReasonInfo.mEnquiryTypeGUIDProperty, result.EnquiryTypeGUID.Value);
                                    LoadProperty(enquiryReasonInfo, Common.EnquiryReasonInfo.mLabelProperty, result.EnquiryReasonLabel);
                                    LoadProperty(enquiryReasonInfo, Common.EnquiryReasonInfo.mAdditionalInfoTextProperty, result.EnquiryReasonAdditionalInfoText);

                                    if (result.CompanyGUID.HasValue)
                                    {
                                        LoadProperty(enquiryReasonInfo, Common.EnquiryReasonInfo.mCompanyGUIDProperty, result.CompanyGUID.Value);
                                    }

                                    // Make sure that CSLA knows this has changed since being instantiated
                                    MarkOld(enquiryReasonInfo);

                                    // Add the item to the list
                                    enquiryReasonInfoList.Add(enquiryReasonInfo);
                                }
                                SetIsReadOnly(enquiryReasonInfoList, true);

                                // Make sure that CSLA knows this has changed since being instantiated
                                MarkOld(enquiryReasonInfoList);

                                return enquiryReasonInfoList;
                            }
                        }
                        catch (InvalidOperationException ioe)
                        {
                            mLog.Error(ioe.ToString());
                            throw ioe;
                        }
                        catch (Exception ex)
                        {
                            mLog.Error(ex.ToString());
                            throw ex;
                        }
                    }

                #endregion

                #region EnquiryActionTypeInfoList

                    /// <summary>
                    /// Method used to fetch a read only list of read only EnquiryActionType objects from the database
                    /// </summary>
                    /// <param name="criteria">The criteria object holding the Guid of the client to retrieve retrieve the action list for</param>
                    /// <returns>A read only list of read only EnquiryActionTypeInfo objects retrieved from the database</returns>
                    public Common.EnquiryActionTypeInfoList FetchActionTypeInfoList(SingleCriteria<Guid> criteria)
                    {
                        try
                        {
                            using (var manager = ContextManager<MUM_dev>.GetManager(Database.BusinessSystem))
                            {
                                // Create an instance to return
                                var enquiryActionTypeInfoList = (Common.EnquiryActionTypeInfoList)MethodCaller.CreateInstance(typeof(Common.EnquiryActionTypeInfoList));

                                // Fetch the data from the database based on the criteria
                                var selectResult = manager.DataContext.UspEnquiryActionTypeSelectByClientGuid(criteria.Value);

                                // EnquiryActionTypeInfoList is a read-only collection,
                                // so typically, when the data access is implemented in
                                // DataPortal_Fetch within the business object, the
                                // private IsReadOnly property must be set false in order to 
                                // populate the collection.  Here, within a factory 
                                // , it isn't visible so one must resort to calling the 
                                // SetIsReadOnly method provided by our parent, ObjectFactory.
                                SetIsReadOnly(enquiryActionTypeInfoList, false);
                                foreach (UspEnquiryActionTypeSelectByClientGuidResult result in selectResult)
                                {
                                    var enquiryActionTypeInfo = (Common.EnquiryActionTypeInfo)MethodCaller.CreateInstance(typeof(Common.EnquiryActionTypeInfo));

                                    // Common properties inherited from MarketPulseBase<EnquiryTypeInfo>
                                    LoadProperty(enquiryActionTypeInfo, Common.EnquiryActionTypeInfo.GUIDProperty, result.EnquiryActionTypeGUID.Value);
                                    LoadProperty(enquiryActionTypeInfo, Common.EnquiryActionTypeInfo.ObsoleteProperty, result.Obsolete.Value);
                                    LoadProperty(enquiryActionTypeInfo, Common.EnquiryActionTypeInfo.InsertDateTimeProperty, result.InsertDateTime.Value);
                                    LoadProperty(enquiryActionTypeInfo, Common.EnquiryActionTypeInfo.InsertUserGUIDProperty, result.InsertUserGUID.Value);
                                    LoadProperty(enquiryActionTypeInfo, Common.EnquiryActionTypeInfo.UpdateDateTimeProperty, result.UpdateDateTime.Value);
                                    LoadProperty(enquiryActionTypeInfo, Common.EnquiryActionTypeInfo.UpdateUserGUIDProperty, result.UpdateUserGUID.Value);
                                    // Other properties                    
                                    LoadProperty(enquiryActionTypeInfo, Common.EnquiryActionTypeInfo.mLabelProperty, result.EnquiryActionTypeLabel);

                                    if (result.CompanyGUID.HasValue)
                                    {
                                        LoadProperty(enquiryActionTypeInfo, Common.EnquiryActionTypeInfo.mCompanyGUIDProperty, result.CompanyGUID.Value);
                                    }

                                    // Make sure that CSLA knows this has changed since being instantiated
                                    MarkOld(enquiryActionTypeInfo);

                                    // Add the item to the list
                                    enquiryActionTypeInfoList.Add(enquiryActionTypeInfo);
                                }
                                SetIsReadOnly(enquiryActionTypeInfoList, true);

                                // Make sure that CSLA knows this has changed since being instantiated
                                MarkOld(enquiryActionTypeInfoList);

                                return enquiryActionTypeInfoList;
                            }
                        }
                        catch (InvalidOperationException ioe)
                        {
                            mLog.Error(ioe.ToString());
                            throw ioe;
                        }
                        catch (Exception ex)
                        {
                            mLog.Error(ex.ToString());
                            throw ex;
                        }
                    }

                #endregion

            #endregion

                    #region Update Methods

                    /// <summary>
                /// Method used to persist a Enquiry object in the database.
                /// </summary>
                /// <param name="program">The Enquiry object to persist.</param>
                /// <returns>A valid (filled) Enquiry object (potentially altered by the act of persisting it).</returns>
                [Transactional(TransactionalTypes.TransactionScope)]
                public Common.Enquiry Update(Common.Enquiry enquiry)
                {
                    using (var manager = ContextManager<MUM_dev>.GetManager(Database.BusinessSystem))
                    {
                        // Set up some local variables to hold the return values from the stored procedure calls
                        Nullable<Guid> userGUID = enquiry.ActionUserGUID;
                        Nullable<DateTime> dateTime = null;

                        // If the object is new, then Insert a record, otherwise Update the current record
                        if (enquiry.IsNew)
                        {
                            // Set up a Guid variable to hold the Guid of the newly inserted Program record
                            Nullable<Guid> guid = null;

                            var enquiryXml = new XElement("enquiry");
                            enquiryXml.SetAttributeValue("datetime", DateTime.Now.ToUniversalTime().ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffK"));
                            enquiryXml.SetAttributeValue("userguid", userGUID.Value);
                            enquiryXml.SetAttributeValue("comment", enquiry.Comment);

                            // Insert the new Program record
                            manager.DataContext.UspEnquiryInsert(ref guid, enquiry.EnquiryTypeGUID,
                                                            enquiry.EnquiryReasonGUID, enquiry.VisitGUID, enquiryXml, userGUID, ref dateTime);
                            // Load the new values into the Program object
                            LoadProperty(enquiry, Common.Enquiry.GUIDProperty, guid.Value);
                            LoadProperty(enquiry, Common.Enquiry.InsertDateTimeProperty, dateTime.Value);
                            LoadProperty(enquiry, Common.Enquiry.InsertUserGUIDProperty, userGUID.Value);
                            LoadProperty(enquiry, Common.Enquiry.UpdateDateTimeProperty, dateTime.Value);
                            LoadProperty(enquiry, Common.Enquiry.UpdateUserGUIDProperty, userGUID.Value);

                            LoadProperty(enquiry, Common.Enquiry.EnquiryXMLProperty, enquiryXml.ToString());
                        }
                        else if (enquiry.IsDirty)
                        {
                            // Update the existing Enquiry record
                            manager.DataContext.UspEnquiryUpdate(enquiry.GUID, enquiry.EnquiryTypeGUID,
                                                            enquiry.EnquiryReasonGUID, enquiry.VisitGUID, enquiry.EnquiryXML, enquiry.Obsolete, userGUID, ref dateTime);

                            // Load the updated values into the Enquiry object
                            LoadProperty(enquiry, Common.Enquiry.UpdateDateTimeProperty, dateTime.Value);
                            LoadProperty(enquiry, Common.Enquiry.UpdateUserGUIDProperty, userGUID.Value);
                        }
                        return enquiry;
                    }
                }

            #endregion

        #endregion
    }
}
